/**
 * Copyright (C), 2001-2031, www.bosssof.com.cn
 * FileName: AnalyzeXmlUtil.java
 * Author: ckj
 * Date: 2024/7/22 下午12:29
 * Description:
 * 解析xml
 * History:
 * Date          Author   Version  Desc
 * 2024-01-01    bosssoft  1.0.0   initialize this file
 */
package com.bosssoft.utils;

import com.bosssoft.constants.FileExtensionConstant;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * @author ckj
 */
public class AnalyzeXmlUtil {
    private static AnalyzeXmlUtil instance;
    private SAXReader saxReader;
    private static final String ANALYZE_FILE = "src/main/java/com/bosssoft/analyzefile/";
    private static final Logger logger = LoggerFactory.getLogger(AnalyzeXmlUtil.class);

    /**
     *
     * @author ckj
     * @date 2024/07/22 下午3:59
     * 私有构造方法，防止外部实例化
     */
    private AnalyzeXmlUtil() {
        saxReader = new SAXReader();
    }

    /**
     *
     * @author ckj
     * @date 2024/07/22 下午4:00
     * 公共的静态方法用于获取单例实例
     * @return com.bosssoft.utils.AnalyzeXmlUtil
     */
    public static synchronized AnalyzeXmlUtil getInstance() {
        if (instance == null) {
            instance = new AnalyzeXmlUtil();
        }
        return instance;
    }

    /**
     * 
     * @author ckj
     * @date 2024/07/23 下午3:03
     * 
 * @param fileName
     * @return org.dom4j.Element 
     */
    public Element getSaxReader(String fileName) {
        Document document = null;
        try {
            document = saxReader.read(fileName);

        } catch (DocumentException e) {
            e.printStackTrace();
        }
        // 3、从文档对象中解析XML文件的全部数据了
        Element root = document.getRootElement();
        return root;
    }

    public List<Element> getElements(Element root) {
        return root.elements();
    }

    public List<Attribute> getAttributes(Element root) {
        return root.attributes();
    }

    /**
     *
     * @author ckj
     * @date 2024/07/23 上午9:23
     * 读取 XML 文档中的元素属性，并将其输出到文件和控制台
     * @param name
     * @param userName
     * @return int 返回 0 表示失败；返回 1 表示成功。
     */
    public int showXml(String name, String userName) {
        String fileName = FileUtil.extractFileName(name);
        String newFileName = ANALYZE_FILE + userName + File.separator + fileName;
        String analyzeFileName = newFileName + FileExtensionConstant.TXT_EXTENSION;

        if (!FileUtil.fileExist(analyzeFileName)) {
            logger.error("分析文件不存在");
            return 0;
        }

        Element root = getSaxReader(name);
        List<Element> elements = getElements(root);

        try (FileWriter fileWriter = new FileWriter(analyzeFileName)) {
            for (Element element : elements) {
                List<Attribute> attributes = getAttributes(element);
                for (Attribute attribute : attributes) {
                    try {
                        fileWriter.write("Attribute " + attribute.getName() + ": " + attribute.getValue() + "\n");
                        System.out.println("Attribute " + attribute.getName() + ": " + attribute.getValue());
                    } catch (IOException e) {
                        logger.error("写入文件失败");
                        return 0;
                    }
                }
            }
            try {
                fileWriter.flush();
            } catch (IOException e) {
                logger.error("刷新文件写入缓冲区失败");
            }
            return 1;
        } catch (IOException e) {
            logger.error("创建文件写入器失败");
            return 0;
        }
    }
}

